import 'package:flutter/material.dart';
import 'package:flutter_getx/utils/token.dart';
import 'package:get/get.dart';
import 'package:my_image/my_image.dart';

import 'logic.dart';
import 'state.dart';

class MinePage extends StatefulWidget {
  const MinePage({super.key});
  @override
  State<MinePage> createState() => _MinePageState();
}

class _MinePageState extends State<MinePage> with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true; // 设置为false，让页面可以被销毁重建
  final MineLogic logic = Get.put(MineLogic());
  final MineState state = Get.find<MineLogic>().state;

  List<Widget> _buildList(List<Map<String, dynamic>> DEL) {
    return DEL.map((Map<String, dynamic> item) {
      return SizedBox(
          height: 50,
          child: Row(children: [
            Row(
              children: [
                Image.asset(
                  ImgRes.imagesPng(item['icon']),
                  width: 20,
                  height: 20,
                ),
                const SizedBox(
                  width: 10,
                ),
                Text(
                  item['title'],
                  style: const TextStyle(fontSize: 16),
                )
              ],
            ),
            const Spacer(),
            const Icon(
              Icons.arrow_forward_ios,
              size: 12,
              color: Colors.grey,
            )
          ]));
    }).toList();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      backgroundColor: const Color.fromARGB(40, 13, 159, 233),
      appBar: AppBar(
          title: const Text('我的', style: TextStyle(color: Colors.white)),
          backgroundColor: Colors.blue,
          centerTitle: true,
          automaticallyImplyLeading: false),
      body: ListView(
        children: [
          // 个人信息
          Container(
              padding: const EdgeInsets.all(10),
              child: GestureDetector(
                  onTap: () {
                    // Navigator.pushNamed(context, '/profile');

                    Get.toNamed('/profile');
                  },
                  child: Row(children: [
                    Row(children: [
                      ClipRRect(
                          borderRadius: BorderRadius.circular(50.0),
                          child: Image.asset(ImgRes.imagesPng('images/avatar_1.jpg'), width: 50, height: 50)),
                      const SizedBox(width: 10),
                      const Text('用户名', style: TextStyle(fontSize: 16, color: Colors.black))
                    ]),
                    const Spacer(),
                    const Row(children: [
                      Text(
                        '去完善信息',
                        style: TextStyle(color: Colors.black),
                      ),
                      Icon(
                        Icons.arrow_forward_ios,
                        color: Colors.black,
                        size: 13,
                      )
                    ])
                  ]))),
          Container(
              padding: const EdgeInsets.all(15),
              margin: const EdgeInsets.only(top: 20, left: 10, right: 10, bottom: 20),
              decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(10)),
              child: Obx(() => Column(children: _buildList(state.menuList as List<Map<String, dynamic>>)))
              //
              ),

          ElevatedButton(
              onPressed: () {
                TokenManager().saveToken('2192189298198219821');
              },
              child: const Text('存储token')),
          const SizedBox(height: 20),
          ElevatedButton(
              onPressed: () {
                TokenManager().removeToken();
              },
              child: const Text('删除token')),

          Obx(() => Text('用户名：${state.userInfo['avatar']}'))
        ],
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    print('MinePage: initState');
    // 每次进入该页面时重新加载数据
    // logic.getProfile();
  }

  @override
  void dispose() {
    print('MinePage: dispose');
    super.dispose();
  }
}
